cap log close 
log using "${logpath}ridelevel_analysis.log", replace

/*******************************************************************************
ridelevel_analysis.do

Produces introductory and counterfactual figures and data on firm exits
*******************************************************************************/

clear all

qui {
	use usrds_id t priorauth_date NPI date ambulance_id year month tot_pmt ///
		using "${cleandatapath}ridelevel.dta", clear

	bysort usrds_id: egen lastride_month=max(t)
	gen stop_at_PA=0 if lastride_month>659 & priorauth_date==1 & t>656 & t<=659
	replace stop_at_PA=0 if lastride_month>672 & priorauth_date==2 & t>669 & t<=672
	replace stop_at_PA=1 if lastride_month>656 & lastride_month<=659 & priorauth_date==1 & t>656 & t<=659
	replace stop_at_PA=1 if lastride_month>669 & lastride_month<=672 & priorauth_date==2 & t>669 & t<=672
	bysort usrds_id stop_at_PA (t): gen PA_stop_ride=stop_at_PA if _n==_N
}

tab PA_stop_ride // Referenced in Section 6.1, Paragraph 4

qui {
	bysort NPI: egen exit_date=max(date) if !missing(NPI)
	gen exit_at_priorauth=(((exit_date==20058 | exit_date==20028) & priorauth_date==1) | ((exit_date==20454 | exit_date==20423) & priorauth_date==2)) if !missing(NPI)
	preserve
	collapse exit_date exit_at_priorauth, by(NPI)
	drop if missing(NPI)
	replace exit_at_priorauth=round(exit_at_priorauth)
	save "${cleandatapath}exit_at_priorauth.dta", replace
	restore
}

preserve
collapse (count) ambulance_id (min) date, by(year month)

sort date
label values date dates
twoway line ambulance_id date if year>=2003, ///
	xti(" ") yti("Monthly Ambulance Rides") ///
	xlab(15706 16802 17898 18993 20089 21185, val) ///
	ylab(, format(%9.0gc)) ///
	graphregion(color(white)) bgcolor(white)
qui graph export "${outpath}Figure_1.pdf", as(pdf) replace // Figure 1
restore

collapse (count) ambulance_id (sum) tot_pmt (min) date, by(year month priorauth_date)
sort date
label values date dates

twoway (line ambulance_id date if priorauth_date==1, lpattern(solid)) ///
	(line ambulance_id date if priorauth_date==2, lpattern(longdash_dot)) ///
	(line ambulance_id date if priorauth_date==0, lpattern(shortdash)) ///
	(pcarrowi 90000 19850 90000 20050, color(black) ) ///
	(pcarrowi 75000 20670 75000 20470, color(black) ), ///
	xline(20058 20454) text( 90000 19850 "1st Reg.", place(w) size(small)) ///
	text( 75000 20670 "2nd Reg.", place(e) size(small)) ///
	xti(" ") yti("Total Monthly Dialysis Ambulance Rides") ///
	xlab(15706 16802 17898 18993 20089 21185, val) ///
	ylab(, format(%9.0gc)) ///
	graphregion(color(white)) bgcolor(white) ///
	leg(subti("Patient State of Residence") ///
		order(1 "NJ, SC, PA" 2 "DE, DC, MD, NC, VA, WV" 3 "No Prior Auth.")) ///
	ti(" ")
qui graph export "${outpath}Figure_2.pdf", as(pdf) replace // Figure 2

qui {
	gen rides_counter=ambulance_id
	su ambulance_id if year<=2004 & priorauth_date==1
	scalar counter=`r(mean)'
	replace rides_counter=min(ambulance_id,counter) if priorauth_date==1
	su ambulance_id if year<=2004 & priorauth_date==2
	scalar counter=`r(mean)'
	replace rides_counter=min(ambulance_id,counter) if priorauth_date==2
	su ambulance_id if year<=2004 & priorauth_date==0
	scalar counter=`r(mean)'
	replace rides_counter=min(ambulance_id,counter) if priorauth_date==0
	egen savedrides=sum(ambulance_id-rides_counter)
	format savedrides %12.0g

	reg ambulance_id c.date##i.priorauth_date if date<20058 & date>=20058-5*365-10
	predict rides_trend
	replace rides_trend=ambulance_id if date<20058
	egen savedrides_trend=sum(rides_trend-ambulance_id)
	format savedrides_trend %12.0g
	
	gen tot_savedrides=savedrides+savedrides_trend
	format tot_savedrides %12.0g

	replace tot_pmt=tot_pmt/1000000

	gen payments_counter=tot_pmt
	su tot_pmt if year<=2004 & priorauth_date==1
	scalar counter=`r(mean)'
	replace payments_counter=min(tot_pmt,counter) if priorauth_date==1
	su tot_pmt if year<=2004 & priorauth_date==2
	scalar counter=`r(mean)'
	replace payments_counter=min(tot_pmt,counter) if priorauth_date==2
	su tot_pmt if year<=2004 & priorauth_date==0
	scalar counter=`r(mean)'
	replace payments_counter=min(tot_pmt,counter) if priorauth_date==0
	egen savedpayments6=sum(tot_pmt-payments_counter)
	format savedpayments %12.0g

	reg tot_pmt c.date##i.priorauth_date if date<20058 & date>=20058-5*365-10
	predict payments_trend
	replace payments_trend=tot_pmt if date<20058
	egen savedpayments_trend=sum(payments_trend-tot_pmt)
	format savedpayments_trend %12.0g
	
	gen tot_savedpayments=savedpayments6+savedpayments_trend
	format tot_savedpayments %12.0g
}

twoway (rarea payments_trend tot_pmt date if priorauth_date==0, fcolor(%30) lpattern(shortdash)) ///
	(rarea payments_trend tot_pmt date if priorauth_date==1, fcolor(%30) lpattern(solid)) ///
	(rarea payments_trend tot_pmt date if priorauth_date==2, fcolor(%30) lpattern(longdash_dot)) ///
	(line payments_trend date if priorauth_date==1, lpattern(solid) lcolor(maroon)) ///
	(line payments_trend date if priorauth_date==2, lpattern(longdash_dot) lcolor(forest_green)) ///
	(line payments_trend date if priorauth_date==0, lpattern(shortdash) lcolor(navy)), ///
	xti(" ") yti("Monthly Ambulance Payments (millions)") ///
	xlab(15706 16802 17898 18993 20089 21185, val) ///
	ylab(, format(%9.0gc)) ///
	graphregion(color(white)) bgcolor(white) ///
	leg(subti("Patient State of Residence") ///
		order(4 "NJ, SC, PA" 5 "DE, DC, MD, NC, VA, WV" 6 "No Prior Auth."))
qui graph export "${outpath}Figure_A21.pdf", as(pdf) replace // Figure A19

twoway (rarea payments_counter tot_pmt date if priorauth_date==0, fcolor(%30) lpattern(shortdash)) ///
	(rarea payments_counter tot_pmt date if priorauth_date==1, fcolor(%30) lpattern(solid)) ///
	(rarea payments_counter tot_pmt date if priorauth_date==2, fcolor(%30) lpattern(longdash_dot)) ///
	(line payments_counter date if priorauth_date==1, lpattern(solid) lcolor(maroon)) ///
	(line payments_counter date if priorauth_date==2, lpattern(longdash_dot) lcolor(forest_green)) ///
	(line payments_counter date if priorauth_date==0, lpattern(shortdash) lcolor(navy)), ///
	xti(" ") yti("Monthly Ambulance Payments (millions)") ///
	xlab(15706 16802 17898 18993 20089 21185, val) ///
	ylab(, format(%9.0gc)) ///
	graphregion(color(white)) bgcolor(white) ///
	leg(subti("Patient State of Residence") ///
		order(4 "NJ, SC, PA" 5 "DE, DC, MD, NC, VA, WV" 6 "No Prior Auth."))
qui graph export "${outpath}Figure_A22.pdf", as(pdf) replace // Figure A20

qui {
	bysort date: egen national_pmt=total(tot_pmt) if !missing(priorauth_date)
	bysort date: egen national_counter=total(payments_counter) if !missing(priorauth_date)
	bysort date: egen national_trend=total(payments_trend) if !missing(priorauth_date)
}

twoway  (rarea national_trend national_counter date if priorauth_date==0, lpatt(none) color(navy%30)) ///
	(line national_pmt date if priorauth_date==0, lpattern(solid) lcolor(navy)) ///
	(line national_trend date if priorauth_date==0, lpattern(longdash_dot) lcolor(navy)) ///
	(line national_counter date if priorauth_date==0, lpattern(shortdash) lcolor(navy)), ///
	xti(" ") yti("Monthly Ambulance Payments (millions)") ///
	xlab(15706 16802 17898 18993 20089 21185, val) ///
	ylab(, format(%9.0gc)) ///
	graphregion(color(white)) bgcolor(white) ///
	leg(order(2 "Actual" 3 "No Prior Auth." 4 "Always Prior Auth." 1 "Counterfactual Savings"))
qui graph export "${outpath}Figure_A23.pdf", as(pdf) replace // Figure A21

list *saved* if _n==1 // Referenced in Section 1, Paragraph 7 and Section 7, Paragraph 1 and Appendix K, Paragraphs 1-3

log close